﻿//////////////////////BEGIN 文件说明///////////////////////////////
// 文件名称: CtnPage.js
// 文件标识: Javascript通用类库
// 包含对象: CtnPage
// 依赖文件: base.js
// 摘    要: 分页处理对象 
//           
//           
//           
// 当前版本: 2.0
// 作    者: zman
// 制作日期: 2008-04-26  武汉
// 修改记录:
//////////////////////END 文件说明////////////////////////////////

//////////////////////BEGIN 简单事例//////////////////////////////
//var cp = null;
//function init()
//{
//    cp = new CtnPage("cp", "page");
//    cp.totalcount = 100;
//    cp.onGoPage = function(page)
//    {
//        this.show();
//    }
//    cp.goPage(1);
//}
//////////////////////END 简单事例////////////////////////////////
function CtnPage(name, id)
{
    this.name = name;
    this.id = id;
    this.curpage = 1;
    this.mode = 1;
    this.styleObj = null;
    this.classname = "ctnpage";
    this.totalcount = 0;
    this.pagesize = 10;
    this.onGoPage = null;
    this.goPage = CtnPage.goPage;
    this.setStyle = CtnPage.setStyle;
    this.show = CtnPage.show;
    this.error = CtnPage.error;
    this.getStyle1 = CtnPage.getStyle1;
    this.getStyle2 = CtnPage.getStyle2;
    this.keyPress = CtnPage.keyPress;
    this.$ = CtnPage.$;
}

CtnPage.Src = {
    firstpage:"首页",
    prevpage:"上页",
    nextpage:"下页",
    lastpage:"末页"
}
CtnPage.$ = function (id){
    if (typeof(id) == "string")
    {
        return document.getElementById(id);
    }
    else
    {
        return id;
    }
}

CtnPage.goPage = function (page)
{
    if (!this.onGoPage)
    {
        return;
    }
    if(page == null)
    {
        page = parseInt(this.$(this.id + "_page").value);
        if (isNaN(page))
        {
            this.error("请填写数字！");
            return;
        }
    }
    if (this.mode && page > this.totalpage)
    {
        page = this.totalpage;
    }
    if(page < 1)
    {
        page = 1;
    }
    this.curpage = page;
    this.onGoPage(page);
}

CtnPage.show = function()
{
    if (this.mode == 1)
    {
        this.$(this.id).innerHTML = this.getStyle1();
    }
    else
    {
        this.$(this.id).innerHTML = this.getStyle2();
    }
}

CtnPage.getStyle1 = function ()
{
    var htmlBuf = new Array();
    htmlBuf.push('<div class="' + this.classname + '">');
    if (this.totalcount > 0)
    {
        this.totalpage = parseInt(this.totalcount / this.pagesize);
        this.totalpage = Math.ceil(this.totalpage);
        this.totalpage < 1 ? 1 : this.totalpage;
        if (this.curpage <= 1)
        {
            htmlBuf.push('<a');
            htmlBuf.push('>' + CtnPage.Src.firstpage + '</a>&nbsp;');
            htmlBuf.push('<a');
            htmlBuf.push('>' + CtnPage.Src.prevpage + '</a>&nbsp;');
        }
        else
        {
            htmlBuf.push('<a');
            htmlBuf.push(' href="javascript:' + this.name + '.goPage(1);">' + CtnPage.Src.firstpage + '</a>&nbsp;');
            htmlBuf.push('<a');
            htmlBuf.push(' href="javascript:' + this.name + '.goPage(' + (this.curpage - 1) + ');">' + CtnPage.Src.prevpage + '</a>&nbsp;');
        }
        if (this.totalcount <= this.curpage * this.pagesize)
        {
            htmlBuf.push('<a');
            htmlBuf.push('>下页</a>&nbsp;');
            htmlBuf.push('<a');
            htmlBuf.push('>尾页</a>&nbsp;');
        }
        else
        {
            htmlBuf.push('<a');
            htmlBuf.push(' href="javascript:' + this.name + '.goPage(' + (this.curpage + 1) + ');">下页</a>&nbsp;');
            htmlBuf.push('<a');
            htmlBuf.push(' href="javascript:' + this.name + '.goPage(' + this.totalpage + ');">尾页</a>&nbsp;');
        }
        if (this.totalpage > 1)
        {
            htmlBuf.push('跳转至');
            htmlBuf.push('<input type="text" size="3" value="' + this.curpage + '"');
            htmlBuf.push(' onkeypress="' + this.name + '.keyPress(event);"');
            htmlBuf.push(' id="' + this.id + '_page"/>&nbsp;');
            htmlBuf.push('<a');
            htmlBuf.push(' href="javascript:' + this.name + '.goPage();">跳转</a>');
        }
        else
        {
            htmlBuf.push('跳转至');
            htmlBuf.push('<input type="text" size="3" disabled="disabled" value="' + this.curpage + '"');
            htmlBuf.push(' id="' + this.id + '_page"/>&nbsp;');
            htmlBuf.push('<span>跳转</span>');
        }
        if (this.totalcount != -1)
        {
            htmlBuf.push('当前: ' + this.curpage + '/' + this.totalpage +'页 ');
            htmlBuf.push('总记录数: ' + this.totalcount + '条 ');
        }
        else
        {
            htmlBuf.push('当前: ' + this.curpage +'页 ');
        }
    }
    else
    {
        htmlBuf.push('没有数据!');
    }
    htmlBuf.push('</div>');
    return htmlBuf.join("");
}
CtnPage.getStyle2 = function ()
{
    var htmlBuf = new Array();
    htmlBuf.push('<div class="' + this.classname + '">');
    if (this.curpage <= 1)
    {
        htmlBuf.push('<a');
        htmlBuf.push('>' + CtnPage.Src.prevpage + '</a>&nbsp;');
    }
    else
    {
        htmlBuf.push('<a');
        htmlBuf.push(' href="javascript:' + this.name + '.goPage(' + (this.curpage - 1) + ');">上页</a>&nbsp;');
    }
    htmlBuf.push('<a');
    htmlBuf.push(' href="javascript:' + this.name + '.goPage(' + (this.curpage + 1) + ');">下页</a>');
    htmlBuf.push('</div>');
    return htmlBuf.join("");
}

CtnPage.keyPress = function(evt){
    evt = evt || window.event;
    if (evt.keyCode == 13){
        this.goPage();
    };
}
CtnPage.error = function(msg)
{
    alert(msg);
}

CtnPage.setStyle = function(val)
{
    if (typeof(val) == "string")
    {
        this.classname = val;
        obj.className = val;
    }
    else if(typeof(val) == "object")
    {
        this.styleobj = val;
        for (var k in val)
        {
            this.$(this.id).style[k] = val[k];
        }
    }
    else if (typeof(val) == "number")
    {
        
    }
}
